package com.zzyl.serve.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.serve.domain.NursingService;
import com.zzyl.serve.mapper.NursingServiceMapper;
import com.zzyl.serve.service.INursingServiceService;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 护理项目Service业务层处理
 * 
 * @author ityu
 * @date 2025-10-11
 */
@Service
public class NursingServiceServiceImpl extends ServiceImpl<NursingServiceMapper,NursingService> implements INursingServiceService
{
    @Autowired
    private NursingServiceMapper nursingServiceMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    private final  String NURSIN_PROJECT_CACHE = "nursing_project_cache";

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingService selectNursingServiceById(Long id)
    {
        return this.getById( id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingService 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingService> selectNursingServiceList(NursingService nursingService)
    {
        return nursingServiceMapper.selectNursingServiceList(nursingService);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingService 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingService(NursingService nursingService)
    {
        deleteCache();
        return this.save(nursingService) == true?1:0;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingService 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingService(NursingService nursingService)
    {
        deleteCache();
        return this.updateById(nursingService) == true?1:0;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingServiceByIds(Long[] ids)
    {
        deleteCache();
        return this.removeByIds(Arrays.asList(ids)) == true?1:0;
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingServiceById(Long id)
    {
        deleteCache();
        return this.removeById( id) == true?1:0;
    }

    @Override
    public List<NursingProjectVo> getAll() {
        List<NursingProjectVo> list = (List<NursingProjectVo>) redisTemplate.opsForValue().get(NURSIN_PROJECT_CACHE);
        if(CollectionUtils.isNotEmpty( list)){
            return list;
        }
        list = nursingServiceMapper.getAll();
        redisTemplate.opsForValue().set(NURSIN_PROJECT_CACHE,list);
        return list;
    }


    private void deleteCache(){
        redisTemplate.delete(NURSIN_PROJECT_CACHE);
    }


}
